package com.zlc.platform.controller;

import com.zlc.common.annotation.LogSaveAnnotation;
import com.zlc.common.common.constant.PlatConstant;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.common.exception.BusinessValidationException;
import com.zlc.common.core.user.model.vo.MayanPlatUserVo;
import com.zlc.common.model.user.ChangePassParam;
import com.zlc.common.model.user.LoginParam;
import com.zlc.common.model.user.UserInfo;
import com.zlc.common.model.user.UserPageRequest;
import com.zlc.common.util.LockerManager;
import com.zlc.platform.model.PermissionEntity;
import com.zlc.platform.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "用户相关")
@RestController
@Slf4j
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private LockerManager lockerManager;


    @ApiOperation(value = "获取当前登录是否需要验证码")
    @PostMapping(value = "/needCheckCode")
    public boolean needCheckCode() {
        return userService.needCheckCode();
    }

    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "登录用户", catalog = "系统登录",logType = 1)
    public UserInfo login(@RequestBody LoginParam loginParam) {
        return userService.login(loginParam.getAccount(), loginParam.getPassword());
    }

    /**
     * 登出
     */
    @ApiOperation(value = "登出")
    @GetMapping(value = "/outLogin")
    public void outLogin() {
        userService.outLogin();
    }

    /**
     * 获取当前用户信息
     */
    @ApiOperation(value = "获取当前用户信息")
    @GetMapping(value = "/get")
    public UserInfo get() {
        return userService.get();
    }


    /**
     * 查询当前用户资源权限
     */
    @ApiOperation(value = "查询当前用户资源权限")
    @GetMapping(value = "/getMenu")
    public PermissionEntity getMenu(@RequestParam(value = "systemCode") String systemCode) {
        return userService.getMenu(systemCode);
    }


    @ApiOperation(value = "查询用户通过主键")
    @GetMapping(value = "/getById")
    public MayanPlatUserVo getById(@RequestParam(value = "id") String id) {
        return userService.getById(id);
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增用户")
    @PostMapping(value = "/add")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "新增用户", catalog = "权限管理/用户管理")
    public void add(@RequestBody MayanPlatUserVo userVo) {
        userService.add(userVo);
    }

    /**
     * 更新用户
     */
    @ApiOperation(value = "更新用户")
    @PostMapping(value = "/update")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "更新用户", catalog = "权限管理/用户管理")
    public void update(@RequestBody MayanPlatUserVo userVo) {
        userService.update(userVo);
    }

    @ApiOperation(value = "更新当前用户")
    @PostMapping(value = "/updateNowUser")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "更新当前用户", catalog = "个人中心")
    public void updateNowUser(@RequestBody MayanPlatUserVo userVo) {
        userService.updateNowUser(userVo);
    }

    /**
     * 重置密码
     */
    @ApiOperation(value = "重置密码")
    @PostMapping(value = "/resetPassword")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "重置密码", catalog = "权限管理/用户管理")
    public void resetPassword(@RequestBody Map<String, String> map) {
        String account = map.get("account");
        if (ObjectUtils.isEmpty(account)) {
            throw new BusinessValidationException("请输入账号");
        }
        userService.resetPassword(account);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/changePassword")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "修改密码", catalog = "个人中心")
    public void changePassword(@RequestBody ChangePassParam param) {
        userService.changePassword(param.getOldPassword(), param.getNewPassword());
    }

    /**
     * 查询用户列表
     */
    @ApiOperation(value = "查询用户列表")
    @PostMapping(value = "/userList")
    public PageVo<MayanPlatUserVo> userList(@RequestBody UserPageRequest request) {
        return userService.userList(request);
    }

    /**
     * 查询用户列表
     */
    @ApiOperation(value = "查询用户列表")
    @GetMapping(value = "/queryUserList")
    public List<MayanPlatUserVo> queryUserList(@RequestParam(value = "key") String key) {
        return userService.queryUserList(key);
    }


    /**
     * 禁用用户
     */
    @ApiOperation(value = "禁用用户")
    @PostMapping(value = "/disabled")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "禁用用户", catalog = "权限管理/用户管理")
    public void disabled(@RequestBody Map<String, String> map) {
        String account = map.get("account");
        if (!ObjectUtils.isEmpty(account)) {
            throw new BusinessValidationException("请输入账号");
        }
        userService.disabled(account);
    }
}
